package xju.rjxy.shop.service.impl;

import xju.rjxy.shop.constant.BaseConstant;
import xju.rjxy.shop.mapper.ReviewMapper;
import xju.rjxy.shop.mapper.impl.ReviewMapperImpl;
import xju.rjxy.shop.model.domain.Review;
import xju.rjxy.shop.model.vo.ReviewVO;
import xju.rjxy.shop.service.IReviewService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 评论服务接口实现类
 */
public class ReviewServiceImpl implements IReviewService {
    private final ReviewMapper reviewMapper = new ReviewMapperImpl();

    /**
     * 获取评论列表
     */
    @Override
    public List<ReviewVO> getReviewList() {
        List<ReviewVO> reviewList = reviewMapper.selectReviewList();
        if (reviewList != null && reviewList.size() > 0) {
            reviewList.forEach(review -> {
                // 处理状态文本值
                review.setFlagValue(getFlagValue(review.getFlag()));
            });
        }
        return reviewList;
    }
    
    /**
     * 根据游戏ID获取评论列表
     */
    @Override
    public List<ReviewVO> getReviewListByGameId(Integer gameId) {
        if (gameId == null) {
            throw new RuntimeException("游戏ID不能为空");
        }
        
        List<ReviewVO> reviewList = reviewMapper.selectByGameId(gameId);
        processReviewList(reviewList);
        return reviewList;
    }
    
    /**
     * 根据用户ID获取评论列表
     */
    @Override
    public List<ReviewVO> getReviewListByUserId(Integer userId) {
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空");
        }
        
        List<ReviewVO> reviewList = reviewMapper.selectByUserId(userId);
        processReviewList(reviewList);
        return reviewList;
    }
    
    /**
     * 添加评论
     */
    @Override
    public boolean addReview(Review review) {
        if (review == null || review.getUserId() == null || review.getGameId() == null) {
            throw new RuntimeException("评论信息不完整");
        }
        if (review.getRating() == null || review.getRating() < 1 || review.getRating() > 5) {
            throw new RuntimeException("评分必须在1-5之间");
        }
        if (review.getComment() == null || review.getComment().trim().isEmpty()) {
            throw new RuntimeException("评论内容不能为空");
        }
        
        // 设置默认值
        review.setCreateTime(LocalDateTime.now());
        review.setFlag(2); // 已通过状态
        review.setStatusCode("APPROVED");
        review.setDeleted(false);
        
        int result = reviewMapper.insertReview(review);
        return result > 0;
    }
    
    /**
     * 更新评论
     */
    @Override
    public boolean updateReview(Review review) {
        if (review == null || review.getId() == null) {
            throw new RuntimeException("评论信息不完整");
        }
        if (review.getRating() != null && (review.getRating() < 1 || review.getRating() > 5)) {
            throw new RuntimeException("评分必须在1-5之间");
        }
        
        int result = reviewMapper.updateReview(review);
        return result > 0;
    }
    
    /**
     * 删除评论
     */
    @Override
    public boolean deleteReview(Integer id) {
        if (id == null) {
            throw new RuntimeException("评论ID不能为空");
        }
        
        int result = reviewMapper.deleteReview(id);
        return result > 0;
    }
    
    /**
     * 根据游戏ID获取平均评分
     */
    @Override
    public Double getAverageRatingByGameId(Integer gameId) {
        if (gameId == null) {
            throw new RuntimeException("游戏ID不能为空");
        }
        
        return reviewMapper.getAverageRatingByGameId(gameId);
    }

    private String getFlagValue(Integer flag) {
        if (flag == null) {
            return BaseConstant.EMPTY;
        }

        // hard code
        switch (flag) {
            case 1:
                return "待审核";
            case 2:
                return "已通过";
            case 3:
                return "已拒绝";
            case 4:
                return "已删除";
            default:
                return BaseConstant.EMPTY;
        }
    }
    
    /**
     * 处理评论列表，设置状态文本值
     */
    private void processReviewList(List<ReviewVO> reviewList) {
        if (reviewList != null && reviewList.size() > 0) {
            reviewList.forEach(review -> {
                review.setFlagValue(getFlagValue(review.getFlag()));
            });
        }
    }
} 